<?php

class Listas_Model_DbTable_Secciones extends Zend_Db_Table_Abstract {
	
	protected $_name = 'seccioneslista_cna';
	protected $_primary = 'codseccionlista';
	protected $_rowClass = 'App_Audit_Row';
	
    public function nextOrden($codlista) {
		$ordenMax = $this->getAdapter()->fetchOne("SELECT MAX(orden) AS cantidad FROM {$this->_name} WHERE codlista={$codlista}");
		if(is_null($ordenMax)) {
			$ordenMax=1;
		} else {
			$ordenMax++;
		}
		return $ordenMax;
	}
	
    public function getSecciones($codlista) {
		$select = $this->select(self::SELECT_WITH_FROM_PART)
						->where("codlista=?", $codlista)
						->order("orden");
		return $select->query()->fetchAll();
	}
	
	public function resetOrden($codlista) {
		$rowset = $this->fetchAll("codlista={$codlista}", "orden");
		$contador=1;
		foreach ($rowset as $row) {
			$row->orden = $contador++;
			$row->save();
		}
	}
	
	/*public function guardarOrden($codsesion, $orden) {
		$this->getAdapter()->beginTransaction();
		try {
			
			$contador=1;
			foreach ($orden as $codagenda) {
				$row = $this->find($codagenda)->current();
				$row->orden = $contador++;
				$row->save();
			}
			$this->getAdapter()->commit();
			
		} catch(Exception $e) {
			$this->getAdapter()->rollBack();
			throw $e;
		}
	}*/
	
}//fin de la clase
